import * as Lodash from 'lodash';

/**
 * 对应字段压缩数据
 * @param data
 * @param nameMap
 * @returns
 */
export const zipData = (
  data: any,
  nameMap: { key: string; label: string }[]
) => {
  const results: any = {};
  const dataKeys = Object.keys(data);
  for (let i = 0; i < nameMap.length; i += 1) {
    const np = nameMap[i];
    if (dataKeys.includes(np.key)) {
      results[np.key] = {
        label: np.label,
        key: np.key,
        value: `${data[np.key]}`,
      };
    }
  }
  return results;
};

/**
 * 渲染树形结构
 * @param flatData
 * @returns
 */
export function childrenDataList<
  T extends { id: string; parentId: string; children?: T[] }
>(flatData: T[]): T[] {
  const idMap = new Map<string, T>();
  const tree: T[] = [];

  flatData.forEach((item) => {
    idMap.set(item.id, { ...item });
  });

  flatData.forEach((item) => {
    const node = idMap.get(item.id)!;
    if (item.parentId && item.parentId !== '' && idMap.has(item.parentId)) {
      const parent = idMap.get(item.parentId)!;
      if (!parent.children) {
        parent.children = [];
      }
      parent.children.push(node);
    } else {
      tree.push(node);
    }
  });
  return tree;
}

/**
 * 数据填充
 * @param target
 * @param source
 * @returns
 */
export const fillData = (target: any, source: any) => {
  return Lodash.assign(target, Lodash.pick(source, Lodash.keys(target)));
};

/**
 * 获取部门列表
 * @param departments
 * @returns
 */
export const getDepartmentList = (departments: any[]) => {
  const dataDepartments: any = {};
  for (let i = 0; i < departments.length; i += 1) {
    const item = departments[i];
    if (item.children) {
      Object.assign(dataDepartments, getDepartmentList(item.children));
    }
    if (!dataDepartments[item.id]) {
      dataDepartments[item.id] = item.name;
    }
  }
  return dataDepartments;
};

/**
 * 获取性别名称
 * @param gender
 * @returns
 */
export const getGenderName = (gender: number): string => {
  if (gender === 1) {
    return '男';
  }
  if (gender === 2) {
    return '女';
  }
  return '未知';
};

/**
 * 从部门列表中获取部门名称
 * @param departments
 * @returns
 */
export const getDepartment = (departments: any[]): string => {
  if (!departments) return '';
  if (departments.length > 0) {
    if (departments[0].departmentName) {
      return departments[0].departmentName;
    }
    return departments[0];
  }
  return '';
};

/**
 * 从职位列表中获取职位名称
 * @param positions
 * @returns
 */
export const getPosition = (positions: any[]): string => {
  if (!positions) return '';
  if (positions.length > 0) {
    if (positions[0].positionName) {
      return positions[0].positionName;
    }
    return positions[0];
  }
  return '';
};

export const renderDatetime = (data: any) => {
  if (Lodash.isString(data)) {
    return data;
  }
  if (!Lodash.isArray(data)) {
    return null;
  }
  if (data.length < 3) {
    return null;
  }
  let datetimeString = `${data[0]} ${data[1]} ${data[2]}`;
  if (data.length > 3) {
    datetimeString += ' ';
    for (let i = 3; i < data.length; i += 1) {
      datetimeString += `${data[i]}:`;
    }
    datetimeString = datetimeString.substring(0, datetimeString.length - 1);
  }
  return datetimeString;
};

/**
 * 通过 value 从列表中获取 label
 * @param value
 * @param options
 * @returns
 */
export const getLabelFromOptions = (value: any, options: any[]) => {
  return options.find((item: any) => item.value === value)?.label;
};

/**
 * 从接口图片对象中获取 arco 图片信息对象
 * @param imgItem 
 * @returns 
 */
export const getInfoFromImageResource = (imgItem: any) => {
  const url = imgItem.resourceUrl as string;
  let uid = imgItem.id as any;
  if (imgItem.resId) {
    uid = imgItem.resId as any;
  }
  const output = {
    uid, url,
    name: '',
  };
  let name = '';
  if (url) {
    if (url.indexOf('?') !== -1) {
      name = url.substring(0, url.indexOf('?'));
      if (name.lastIndexOf('/') !== -1) {
        name = name.substring(name.lastIndexOf('/') + 1);
      }
    }
  }
  if (!name) {
    if (imgItem.resourceType) {
      name += `${imgItem.resourceType}-`;
    }
    name += `imgItem.id`;
  }
  output.name = name;
  return output;
};

/**
 * 从 arco 图片对象列表中获图片 ID 列表
 * @param imgItems 
 * @returns 
 */
export const getIdsFromArcoImages = (imgItems: any[]) => {
  const outputs: number[] = [];
  for(let i=0; i<imgItems.length; i+=1) {
    outputs.push(imgItems[i].uid);
  }
  return outputs;
};
